#ifndef WEBCAMFRAMESOURCE_H
#define WEBCAMFRAMESOURCE_H

#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
#include <GroupsockHelper.hh>

// 使用 webcam + x264
class WebcamFrameSource : public FramedSource
{
private:
    struct Buffer
    {
        void *start;
        size_t length;
    };
    int vid;//设备句柄
    Buffer bufs[3];//mmap 缓存

    int m_started;
    void *mp_token;
public:
    WebcamFrameSource(UsageEnvironment &env);
    ~WebcamFrameSource();
protected:
    virtual void doGetNextFrame();
private:
    static void getNextFrame(void *ptr);
    void getNextFrame1();

    int capture_open(const char *dev_name, int width, int height, int fps);
    void capture_close();
};

class WebcamMediaSubsession : public OnDemandServerMediaSubsession
{
public:
    WebcamMediaSubsession(UsageEnvironment &env);
    virtual ~WebcamMediaSubsession();
private:
    static void chkForAuxSDPLine(void *ptr);
    void chkForAuxSDPLine1();
protected:
    virtual char const *getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource);
    virtual RTPSink *createNewRTPSink(Groupsock *rtpsock, unsigned char type, FramedSource *source);
    virtual FramedSource *createNewStreamSource(unsigned /*clientSessionId*/, unsigned& estBitrate);

private:
  char* fAuxSDPLine;
  char fDoneFlag; // used when setting up "fAuxSDPLine"
  RTPSink* fDummyRTPSink; // ditto
};

#endif // WEBCAMFRAMESOURCE_H
